$(function() {
	init();
	//js中导入客户信息
	nameInput();
	//出生日期
	var opt = {
        preset: 'date', //日期
        theme: 'android-ics light', //皮肤样式
        display: 'bottom', //显示方式 
        mode: 'scroller', //日期选择模式
        lang: 'zh',
        dateFormat: 'yy-mm-dd', // 日期格式
        setText: '确定', //确认按钮名称
        cancelText: '取消',//取消按钮名籍我
        dateOrder: 'yymmdd', //面板中日期排列格式
        dayText: '', monthText: '', yearText: '', //面板中年月日文字
        endYear:2020, //结束年份
        onSelect:function(valueText, inst){
        	console.log(valueText);
        	$(this).next().val(valueText);
        	OnDateOfBirthChange(valueText,this);
        }
	};
	$('.input-group-addon').mobiscroll(opt);
/*	$('.input-group input:first').change(function(){
		$(this).parent().find('input:last').val(null);
		$('.input-group-addon').mobiscroll('setDate',new Date());
	});*/
});

var productList = null;

function init() {
	var jsonParam = {};
	jsonParam.pkgId = getArgsFromHref("pkgId");
	padMarketing.ajaxNormal("/pad_marketing/main/pkgGetRiders.action",jsonParam,
		function (data){
			var tip = '<h3 class="font-16 white"><span class="icon icon-add padding-right-15"><label class="white font-18">主</label></span>&nbsp;';
			$("#product-title").html(tip + data.title);
			showData(data.riders);
			productList = data;
			//附加险列表注册检查事件，选中一个会检查是否与其他已选中的附加险互斥
			$('input[data-product]').click(selectedAndExclusive);
		},
		function(data){
			
		}
	);
}

function showData(data) {
	var thumbnail = $("#tpl_thumbnail").html();
	var thumbnail_list = [];
	var list = [];
	for(var i = 0; i < data.length; i++) {
		if(!list[data[i].category]) {
			list[data[i].category] = [];
		}
		list[data[i].category].push(data[i]);
	}
	for(title in list) {
		var product = list[title];
		if(!thumbnail_list[title]) {
			thumbnail_list[title] = [];
		}
		for(var i = 0; i < product.length; i++) {
			var thumbnail_html = thumbnail.replace(/\{title\}/g, product[i].coverName.substring(2, product[i].coverName.length))
			  							  .replace(/\{pkgId\}/g, product[i].coverCode)
			  							  .replace(/\{subTitle\}/g, product[i].subTitle)
			  							  .replace(/\{product\}/g, "data-product='" + JSON.stringify(product[i]) + "'");
			thumbnail_list[title].push(thumbnail_html);
		}
	}	
	if(thumbnail_list['H']) {
		$("#health").html(thumbnail_list['H'].join(''));
	}
	if(thumbnail_list['R']) {
		$("#old").html(thumbnail_list['R'].join(''));
	}
	if(thumbnail_list['E']) {
		$("#edu").html(thumbnail_list['E'].join(''));
	}
	if(thumbnail_list['F']) {
		$("#plan").html(thumbnail_list['F'].join(''));
	}
	if(thumbnail_list['L']) {
		$("#life").html(thumbnail_list['L'].join(''));
	}
}

//勾选附加险时，提示互斥
function selectedAndExclusive(){
	//附加险待选列表,可选的
	var riderCovers = $('input[data-product]:checked');
	var curCover = $(this).data('product');
	if(curCover.exclusiveGroup){
		for(var i=0;i<riderCovers.length;i++){
			var cover = riderCovers.eq(i).data('product');
			//先排除自身
			if(curCover.coverCode==cover.coverCode)
				continue;
			if(cover.exclusiveGroup&&extractRepeat(curCover.exclusiveGroup.split(','),cover.exclusiveGroup.split(',')).length>0){
				alertMsg('当前险种【'+curCover.coverName+'】与您已选中的【'+cover.coverName+'】互斥，如需勾选此险种，请先取消【'+cover.coverName+'】');
				$(this).removeAttr("checked");
			}
		}
	}
}

function complete(index){
	//以下是判断是否勾选产品的。。。。。
	/*if($(".check-list  input:checkbox:checked").length==0){
		alertMsg("请您选择产品");
		return;
	}else{
		
	}	*/
	//点击建议书 或立即购买的标示位
	$("#normalSure").removeAttr("index");
	$("#normalSure").attr("index",index);
	var i = index==2?1:2;
	showDiv('open'+i);
}

function toNext(index) {
	var mins = {};
	var inputs = $('#open' + index).find('input');
	mins.name = inputs.eq(0).val();
	var re = /^\d*$/;
	if(mins.name == "" || mins.name == null) {
		alertMsg("请正确输入姓名");
		return;
	}
	if(index == 1) {
		mins.dateOfBirth = inputs.eq(1).val();
		if(inputs.eq(2).parent().hasClass('active')){
			mins.sex = 'M';
		}else{
			mins.sex = 'F';
		}
		mins.ocCode = inputs.eq(4).data('selected').code;
	}
	if(index == 2) {
		mins.age = inputs.eq(1).val();
		mins.dateOfBirth = inputs.eq(2).val();
		if(!re.test(mins.age)||mins.age==""||mins.age==null) {
			alertMsg("请正确输入年龄");
			return ;
		}
		if(inputs.eq(3).parent().hasClass('active')){
			mins.sex = 'M';
		}else{
			mins.sex = 'F';
		}
		mins.ocCode = inputs.eq(5).data('selected').code;
	}
	
	productList.riders = [];
	$("input[name='product']:checked").each(function() {
//		console.log($(this).data("product"));
		productList.riders.push($(this).data("product"));
	});
//	console.log(productList);
	var jsonParam = {};
	jsonParam.pkgs = [productList];
	jsonParam.mins = mins;
	console.log(jsonParam);
	var url = "/pad_marketing/main/appCreateWithRiders.action";
	if(index == 2) {
		url = "/pad_marketing/main/quoCreateWithRiders.action";
	}
	padMarketing.ajaxNormal(url, jsonParam,
		function (data){
			console.log(data);
			if(index == 1) {
				sessionStorage.appBase = JSON.stringify(data[0]);
				window.location.href = "/pad_marketing/purchaseNote/index.action";
			} else if(index == 2) {
				sessionStorage.quoBase = JSON.stringify(data);
				sessionStorage.returnUrl = "/pad_marketing/main/listViewStar.action?pkgId=112";
				window.location.href = '/pad_marketing/plan/proposalNew.action';
			}
		},
		function(data){
			
		}
	);
}

$(document).on('click', "input[type='checkbox']", function() {
	var inputCheck = $(this).parent().parent().parent();
	var id = inputCheck.attr("id");
	setTimeout(function(){
/*		var idNum = parseInt($("#" + id + "-num").text());
		if(inputCheck.is(':checked')) {
			idNum++;
		} else {
			idNum--;
		}*/
		$("#" + id + "-num").text(inputCheck.find('input[type="checkbox"]:checked').length);
	},200);
});

$(document).on('click','.thumbnail',function(){
	//获得所选产品的基本信息
	$(".tabs-line a").eq(0).addClass("active").siblings().removeClass("active");
	$("#open5 .tabSwiper .swiper-wrapper .swiper-slide").eq(0).addClass("swiper-slide-active").siblings().removeClass("swiper-slide-active");
	$("#open5 .tabSwiper .swiper-wrapper").css({"transform":" translate3d(0px, 0px, 0px)","transition-duration": "0 s"});
	var inputs = $(this).find('input');
	var div = eval('(' +$(inputs).attr('data-product') + ')');
	var imgsrc = resoucesPath + 'images/product/info/CPTS/'+div.coverCode+'.jpg';
	$('.show-pic').find('img').attr('src',imgsrc);
	var imgsrc1 =  resoucesPath + 'images/product/info/LYJJ/'+div.coverCode+'.jpg';		
	$('.relative').find('img').attr('src',imgsrc1);
	$("#title").html(div.coverName+'      (投保年龄:'+div.subTitle+')');
	//附加险详情
	var url= resoucesPath + "material/riders/";
	//特色
	var flag=true;
	$.get(url+div.coverCode+"_feature.html",function(html){
		if(html){
			$("#feature").html(html);
		}else{
			flag=false;
		}
	});
	//利益
	$.get(url+div.coverCode+"_benefit.html",function(html){		
		if(html){
			$("#benefit").html(html);
		}else{
			flag=false;
		}
	});	
	    if(flag){
		showDiv('open4');
	}
})

$(document).on('click','.check-list',function(event){
	 event.stopPropagation();
})